package com.linuxgt.android.service;

import android.util.Xml;

import com.linuxgt.android.common.Const;
import com.linuxgt.android.domain.Person;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liute on 2017/5/15.
 */

public class PersonService {

    /**
     * 从xml中获取个人信息
     *
     * @param inputStream 输入流对象
     * @return
     * @throws Exception
     */
    public List<Person> getPersons(InputStream inputStream) throws Exception {
        List<Person> personList = null;
        Person person = null;
        XmlPullParser xmlPullParser = Xml.newPullParser();
        xmlPullParser.setInput(inputStream, "UTF-8");
        int event = xmlPullParser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
                case XmlPullParser.START_DOCUMENT:
                    personList = new ArrayList<>();
                    break;
                case XmlPullParser.START_TAG:
                    if ("person".equals(xmlPullParser.getName())) {
                        String id = String.valueOf(xmlPullParser.getAttributeValue(0));
                        person = new Person();
                        person.setId(id);
                    }
                    if ("name".equals(xmlPullParser.getName())) {
                        String name = String.valueOf(xmlPullParser.nextText());
                        person.setName(name);
                    }
                    if ("age".equals(xmlPullParser.getName())) {
                        Integer age = Integer.valueOf(xmlPullParser.nextText());
                        person.setAge(age);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("person".equals(xmlPullParser.getName())) {
                        personList.add(person);
                        person = null;
                    }
                    break;
            }
            event = xmlPullParser.next();
        }
        return personList;
    }

    /**
     * 保存数据到xml
     *
     * @param personList
     * @param outputStream
     * @throws Exception
     */
    public void save(List<Person> personList, OutputStream outputStream) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        xmlSerializer.setOutput(outputStream, Const.DEFAULT_ENCODING);
        xmlSerializer.startDocument(Const.DEFAULT_ENCODING, true);
        xmlSerializer.startTag(null, "persons");
        for (Person person : personList) {
            xmlSerializer.startTag(null, "person");
            xmlSerializer.attribute(null, "id", person.getId());

            xmlSerializer.startTag(null, "name");
            xmlSerializer.text(person.getName());
            xmlSerializer.endTag(null, "name");

            xmlSerializer.startTag(null, "age");
            xmlSerializer.text(String.valueOf(person.getAge()));
            xmlSerializer.endTag(null, "age");

            xmlSerializer.endTag(null, "person");
        }
        xmlSerializer.endTag(null, "persons");
        xmlSerializer.endDocument();
    }

    /**
     * 保存数据到xml
     *
     * @param objectList
     * @param outputStream
     * @throws Exception
     */
    public void saveObject(List<?> objectList, OutputStream outputStream) throws Exception {
        String namespace = null;
        String className = objectList.get(0).getClass().getSimpleName();
        XmlSerializer xmlSerializer = Xml.newSerializer();
        xmlSerializer.setOutput(outputStream, Const.DEFAULT_ENCODING);
        xmlSerializer.startDocument(Const.DEFAULT_ENCODING, true);
        xmlSerializer.startTag(namespace, className + "s");
        for (Object o : objectList) {
            xmlSerializer.startTag(namespace, className);
            Class<?> objectClass = o.getClass();
            Field[] fields = objectClass.getDeclaredFields();
            for (Field field : fields) {
                createXmlObjectStr(namespace, xmlSerializer, field, o);
            }
            xmlSerializer.endTag(namespace, className);
        }
        xmlSerializer.endTag(namespace, className + "s");
        xmlSerializer.endDocument();
    }

    private void createXmlObjectStr(String namespace, XmlSerializer xmlSerializer, Field field, Object object) throws IOException, IllegalAccessException {
        xmlSerializer.startTag(namespace, field.getName());
        field.setAccessible(true);
        Object f = field.get(object);
        if (f instanceof String || f instanceof Integer) {
            xmlSerializer.text(String.valueOf(f));
        } else {
            xmlSerializer.text("");
        }
        xmlSerializer.endTag(namespace, field.getName());
    }
}
